﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSRedis;
using Microsoft.AspNetCore.Mvc.Formatters;
using NetMicro.Cache.Enums;

namespace NetMicro.Cache.Extensions
{
    internal static class RedisExtensions
    {
        /// <summary>
        /// csredis 条件转换
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        internal static RedisExistence ToWhen(this RedisWhen type)
        {
            switch (type)
            {
                case RedisWhen.Exists:
                    return RedisExistence.Xx;

                case RedisWhen.NotExists:
                    return RedisExistence.Nx;
                default:
                    throw new RedisException("RedisProxy 非法的条件转换");
            }
        }

        /// <summary>
        /// csredis 类型转换
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        internal static RedisItemType ToType(this KeyType type)
        {
            return (RedisItemType)(int)type;
        }
        /// <summary>
        /// 聚合转换
        /// </summary>
        /// <param name="aggregate"></param>
        /// <returns></returns>
        internal static CSRedis.RedisAggregate ToAggregate(this Enums.RedisAggregate aggregate)
        {
            switch (aggregate)
            {
                case Enums.RedisAggregate.Sum:
                    return CSRedis.RedisAggregate.Sum;

                case Enums.RedisAggregate.Max:
                    return CSRedis.RedisAggregate.Max;

                case Enums.RedisAggregate.Min:
                    return CSRedis.RedisAggregate.Min;

                default:
                    throw new RedisException("RedisProxy 非法的聚合运算");
            }
        }
        /// <summary>
        /// 排序转换
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        internal static RedisSortDir ToOrder(this RedisOrder order)
        {
            switch (order)
            {
                case RedisOrder.Ascending:
                    return RedisSortDir.Asc;

                case RedisOrder.Descending:
                    return RedisSortDir.Desc;

                default:
                    throw new RedisException("RedisProxy 非法顺序");
            }
        }
    }
}
